package com.usian.interceptor;

/**
 * @author chen
 * @date 2021/1/27 - 21:59 - 星期三
 * @project usian_parent
 * @package com.usian.interceptor
 * @by GGbomd猪猪侠
 */

import com.usian.feign.SSOServiceFeign;
import com.usian.pojo.TbUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 结算之前判断用户是否已经登录
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    @Autowired
    private SSOServiceFeign ssoServiceFeign;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 对用户的token做判断  看用户是否登录过
        String token = request.getParameter("token");
        if (StringUtils.isBlank(token)){
            return false;
        }
        // 如果用户token不为空 则校验用户在redis中是否已经失效
        TbUser userByToken = ssoServiceFeign.getUserByToken(token);
        if (userByToken == null){
            return false;
        }
        return true;
    }
}
